package com.wzq.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.support.multidex.MultiDex;

import com.google.gson.Gson;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
import com.wzq.app.manager.ActivityStackManager;
import com.wzq.app.utils.FilePathUtils;
import com.wzq.app.utils.SPUtils;
import com.wzq.app.utils.Utils;

import java.lang.ref.WeakReference;


/**
 * @author:ruchen 功能描述:
 * 创建日期:2017/3/22
 * 修改者，修改日期，修改内容。
 */
public class BusApplication extends Application  {
    public static BusApplication mApplicationContext ;
    public Gson gson;

    public static boolean isHome = false;


    @Override
    public void onCreate() {
        super.onCreate();
        mApplicationContext = this;
        ZXingLibrary.initDisplayOpinion(this);
        SPUtils.init(this);
        Utils.init(this);
        FilePathUtils.initFilePath(this);
        gson = new Gson();
        registerActivityLifecycleCallbacks(new SwitchBackgroundCallbacks());
    }
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);

    }
    public static BusApplication getAppContext() {
        return mApplicationContext;
    }

    public static Gson getGson(){
        return mApplicationContext.gson;
    }


    private class SwitchBackgroundCallbacks implements ActivityLifecycleCallbacks {

        @Override
        public void onActivityCreated(Activity activity, Bundle bundle) {
            ActivityStackManager.getInstance().addActivity(new WeakReference(activity));
        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            ActivityStackManager.getInstance().removeActivity(new WeakReference(activity));
        }
    }


}
